package com.liukai.exam.controller;


import com.liukai.exam.common.Result;
import com.liukai.exam.pojo.ExamManage;
import com.liukai.exam.service.IExamManageService;
import com.liukai.exam.utils.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.bind.ValidationEvent;
import java.util.List;

/**
 * <p>
 * 试卷管理 前端控制器
 * </p>
 *
 * @author liu kai
 * @since 2021-02-01
 */
@RestController
@RequestMapping("/exam/examManage")
@Slf4j
public class ExamManageController {

    @Autowired
    private IExamManageService iExamManageService;

    /**
     * 新增试卷信息
     *
     * @param examManage
     * @return
     */
    @PostMapping("/addExamManage")
    public Result addExamManage(@RequestBody ExamManage examManage) {

        log.info(examManage.toString());
        int count = iExamManageService.addExamManage(examManage);
        log.info("count" + count);
        if (count > 0) {
            ExamManage newExamManage = iExamManageService.findExamNameByName(examManage.getTeacherId(),
                    examManage.getClassId(), examManage.getExamName());
            Result result = Result.succ(200, "新增试卷信息成功", newExamManage);
            return result;
        } else if (count == Constant.EXAM_MANAGE_ADD_FILED_NAME_EXIST) {
            Result result = Result.fail(404, "新增试卷信息失败,试卷名称已存在", null);
            return result;
        } else {
            Result result = Result.fail(404, "新增试卷信息失败", null);
            return result;
        }
    }

    /**
     * 根据账号信息查询未完成创建的试卷信息
     * @param userId
     * @param classId
     * @return
     */
    @GetMapping("/findNoDoneCreateExam/{userId}/{classId}")
    public Result findNoDoneCreateExam(@PathVariable Long userId, @PathVariable Integer classId) {

        log.info("findNoDoneCreateExam");
        List<ExamManage> noDoneCreateExam = iExamManageService.findNoDoneCreateExam(userId, classId);

        return Result.succ(200,"获取未完成创建的试卷成功",noDoneCreateExam);
    }

    /**
     * 根据账号信息查询已完成创建的试卷信息
     * @param userId
     * @param classId
     * @return
     */
    @GetMapping("/findDoneCreateExam/{userId}/{classId}")
    public Result findDoneCreateExam(@PathVariable Long userId, @PathVariable Integer classId) {

        log.info("findDoneCreateExam");
        List<ExamManage> doneCreateExam = iExamManageService.findDoneCreateExam(userId, classId);

        return Result.succ(200,"获取已经完成创建的试卷成功",doneCreateExam);
    }

}

